//
//  File.swift
//  
//
//  Created by WeCloudIOS on 2022/4/14.
//

import Foundation
import UIKit

class UserListView : UIView{
    
    lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.frame = CGRect(x: 0, y: 0, width: Windos.w, height: 80)
        return scrollView
    }()
    
    var ObservableRoom:WCObservableRoom?
    var trackArray = [Any]()
    var defautView: VideoDefaultView?
    var room: RoomListener?
    var userData: NSDictionary?
    init() {

        super.init(frame: CGRect.zero)
        addSubview(self.scrollView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func show(sid:String?){
        setUI(sid:sid)
    }
    
    func setUI(sid:String?){
        let w = ObservableRoom!.remoteParticipants.count-1 * 60 + ObservableRoom!.remoteParticipants.count-1 * 10+10
        scrollView.contentSize = CGSize(width: w, height: 0)
        
        for view in scrollView.subviews{
            view.removeFromSuperview()
        }
        
        addVideoView(sid: sid)
    }
    
    func addVideoView(sid: String?){
        var index = 0
        for (key,value) in ObservableRoom!.remoteParticipants.elements{
            
            let smallView = VideoSmallListView()
            smallView.tag = index
            let space = 10+index*60+index*10
            smallView.frame = CGRect(x: space, y: 0, width: 60, height: 60)
            scrollView.addSubview(smallView)
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction(_ :)))
            smallView.addGestureRecognizer(tap)
            
            let name = UILabel()
            name.textAlignment = .center
            name.text = value.identity
            name.textColor = UIColor.white
            name.font = UIFont.systemFont(ofSize: 12)
            name.frame = CGRect(x: space, y: 60, width: 60, height: 20)
            scrollView.addSubview(name)
            
            //判断是否可以显示
            if value.firstVideoAvailable{
                
                if sid == key{ //判断是否已经在大图显示
//                    smallView.topView.isHidden = false
                    smallView.sid = key
//                    smallView.state.text = "正在显示"
                    setUser(userId: value.identity,nickLab: name,view: smallView,isShow: true)
                }else{
                    smallView.sid = key
//                    smallView.topView.isHidden = true
                    smallView.videoData = value.firstCameraVideoTrack
                    setUser(userId: value.identity,nickLab: name,view: smallView,isShow: false)
                }
                
            }else{
//                smallView.topView.isHidden = false
                smallView.sid = nil
//                smallView.state.text = "视频未开"
                setUser(userId: value.identity,nickLab: name,view: smallView,isShow: true)
            }
            
            
            
            index += 1
        }
    }
    
    @objc func tapAction(_ obj:UITapGestureRecognizer){
        
        let view = obj.view as! VideoSmallListView
        
        if view.videoData != nil{
            defautView?.setNewMaxVideo(track: view.videoData!)
            addVideoView(sid: view.sid)
        }
    }
    
    func setUser(userId: String,nickLab: UILabel,view: VideoSmallListView,isShow: Bool) {
        
        if userData != nil{
            if let id = userData!.object(forKey: "userId") as? String ,id == userId{
                nickLab.text = userData!.object(forKey: "nickname") as? String
                let link:String? = userData!.object(forKey: "headPortrait") as? String
                if userData!.object(forKey: "headPortrait") != nil {
                    
                    if isShow{
                        view.coverImgage.isHidden = false
                        if link != nil{
                            view.userHeaderUrl = link!
                        }else{
                            view.coverImgage.image = Tool.SCImage(named: "firm_evaluation_avatar")
                        }
                    }else{
                        view.coverImgage.isHidden = true
                    }
 
                }else{
                    if isShow{
                        view.coverImgage.image = Tool.SCImage(named: "firm_evaluation_avatar")
                        view.coverImgage.isHidden = false
                    }else{
                        view.coverImgage.isHidden = true
                    }
                }
                
                return
            }
        }
        
        let params:NSMutableDictionary = NSMutableDictionary()
        params["userId"] = userId
        
        NetWork(url: "/api/user/getUserByUserId", params: params) { obj in
            let data: NSDictionary = obj as! NSDictionary
            self.userData = data
            DispatchQueue.main.async {
                nickLab.text = data.object(forKey: "nickname") as? String
                let link:String? = data.object(forKey: "headPortrait") as? String
                
                if data.object(forKey: "headPortrait") != nil {
                    
                    if isShow{
                        view.coverImgage.isHidden = false
                        if link != nil{
                            view.userHeaderUrl = link!
                        }else{
                            view.coverImgage.image = Tool.SCImage(named: "firm_evaluation_avatar")
                        }
                    }else{
                        view.coverImgage.isHidden = true
                    }
 
                }else{
                    if isShow{
                        view.coverImgage.image = Tool.SCImage(named: "firm_evaluation_avatar")
                        view.coverImgage.isHidden = false
                    }else{
                        view.coverImgage.isHidden = true
                    }
                }
            }
        }
    }
}
